package com.xca.bindingwork.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xca.bindingwork.R;
import com.xca.bindingwork.adapter.PetsAdapter;
import com.xca.bindingwork.contact.PetContact;
import com.xca.bindingwork.databinding.ActivityMainBinding;
import com.xca.bindingwork.entity.PetEntity;
import com.xca.bindingwork.viewModel.PetListModel;

import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity{
    private ActivityMainBinding binding ;
    private PetListModel petListModel ;
    private ViewModelProvider viewModelProvider ;
    private MutableLiveData<PetEntity> liveData ;
    private PetsAdapter adapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main) ;
        viewModelProvider = new ViewModelProvider(this) ;
        petListModel = viewModelProvider.get(PetListModel.class) ;

        liveData = (MutableLiveData<PetEntity>) petListModel.getLiveData() ;
        RecyclerView recyclerView = binding.recyclerView ;
        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
        binding.type1.setSelected(true);
        liveData.observe(this, new Observer<PetEntity>() {
            @Override
            public void onChanged(PetEntity petEntity) {
                if (petEntity == null){
                    Toast.makeText(MainActivity.this, "未查询到该品种", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (petEntity.getResult().getList().size() == 1){
                    PetEntity.ResultBean.ListBean pet = petEntity.getResult().getList().get(0) ;
                    int type = pet.getPettype() ;
                    if (type == 0){
                        binding.type1.setSelected(true);
                        binding.type2.setSelected(false);
                        binding.type3.setSelected(false);
                        binding.type4.setSelected(false);
                        binding.type5.setSelected(false);
                    }else if (type == 1){
                        binding.type1.setSelected(false);
                        binding.type2.setSelected(true);
                        binding.type3.setSelected(false);
                        binding.type4.setSelected(false);
                        binding.type5.setSelected(false);
                    }else if (type == 2){
                        binding.type1.setSelected(false);
                        binding.type2.setSelected(false);
                        binding.type3.setSelected(true);
                        binding.type4.setSelected(false);
                        binding.type5.setSelected(false);
                    } else if (type == 3) {
                        binding.type1.setSelected(false);
                        binding.type2.setSelected(false);
                        binding.type3.setSelected(false);
                        binding.type4.setSelected(true);
                        binding.type5.setSelected(false);
                    } else if (type == 4) {
                        binding.type1.setSelected(false);
                        binding.type2.setSelected(false);
                        binding.type3.setSelected(false);
                        binding.type4.setSelected(false);
                        binding.type5.setSelected(true);
                    }
                }
                // List<PetEntity.ResultBean.ListBean> list = Objects.requireNonNull(petListModel.getLiveData().getValue()).getResult().getList() ;
                adapter = new PetsAdapter(petEntity.getResult().getList(),MainActivity.this,MainActivity.this) ;
                Log.d("获取到的数据长度", String.valueOf(petEntity.getResult().getList().size()));
                recyclerView.setAdapter(adapter);
            }
        });

        binding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                if (s==null){
                    Toast.makeText(MainActivity.this, "搜索内容为空", Toast.LENGTH_SHORT).show();
                    return false;
                }
                Log.d("搜索内容", s);
                petListModel.getData(s,7);
                return false; // 返回true不会触发搜索
            }

            @Override
            public boolean onQueryTextChange(String s) {// 搜索框内容改变时触发
                return false;// 返回true不会触发搜索
            }
        });
        binding.setListener(new SelectClickListener());
        petListModel.getData(null,0);

    }


    public class SelectClickListener {
        public void onClick(View view) {
            int id = view.getId() ;
            String name = null ;
            int type = 0 ;
            if (id == R.id.type1){
                binding.type1.setSelected(true);
                binding.type2.setSelected(false);
                binding.type3.setSelected(false);
                binding.type4.setSelected(false);
                binding.type5.setSelected(false);
                type = 0 ;
            }else if (id == R.id.type2){
                type = 1 ;
                binding.type1.setSelected(false);
                binding.type2.setSelected(true);
                binding.type3.setSelected(false);
                binding.type4.setSelected(false);
                binding.type5.setSelected(false);
            } else if (id == R.id.type3) {
                binding.type1.setSelected(false);
                binding.type2.setSelected(false);
                binding.type3.setSelected(true);
                binding.type4.setSelected(false);
                binding.type5.setSelected(false);
                type = 2 ;
            } else if (id == R.id.type4) {
                binding.type1.setSelected(false);
                binding.type2.setSelected(false);
                binding.type3.setSelected(false);
                binding.type4.setSelected(true);
                binding.type5.setSelected(false);
                type = 3 ;
            } else if (id == R.id.type5) {
                binding.type1.setSelected(false);
                binding.type2.setSelected(false);
                binding.type3.setSelected(false);
                binding.type4.setSelected(false);
                binding.type5.setSelected(true);
                type = 4 ;
            }
            petListModel.getData(null,type);
            Log.d("搜索类型", String.valueOf(type));
        }
    }
}